package com.ctsi.commons.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模拟xml为property
 * 
 * @author yueming
 * 
 */
public class XmlProperty {
	private static final Logger log = LoggerFactory.getLogger(XmlProperty.class);
	private static final Map<String, String> cache = new HashMap<String, String>();

	private String resName;

	public XmlProperty(String resName) {
		this.resName = resName;

	}

	public String getPropertyInCache(String key) {
		return getPropertyInCache(resName, key);
	}

	private static String getResourceName(String resourceName) {
		if (resourceName.indexOf(".") < 0)
			return resourceName + ".xml";
		else {
			if (resourceName.indexOf(".xml") < 0) {
				return resourceName.replace('.', '/') + ".xml";
			} else {
				String cutProperties = resourceName.substring(0, resourceName.indexOf(".xml"));
				String tem = cutProperties.replace('.', '/') + ".xml";
				// String
				// result=tem.substring(0,tem.lastIndexOf("/")-1)+"."+tem.substring(tem.lastIndexOf("/")+1);
				return tem;
			}

		}
	}

	public static String getPropertyInCache(String resourceName, String key) {

		assert (resourceName != null);

		String cacheKey = resourceName.concat(key);
		String result = cache.get(cacheKey);
		if (result == null) {
			synchronized (XmlProperty.class) {
				cache.putAll(getPropertiesMap(resourceName));

				result = cache.get(cacheKey);

			}
		}

		return result;

	}

	public static String getProperty(String resourceName, String key) {

		return getPropertiesMap(resourceName).get(resourceName.concat(key));

	}
	public static String getProperty(URL resourceName, String key) {

		return getPropertiesMap(resourceName).get(resourceName.toString().concat(key));

	}
	public synchronized static void clearCache() {

		cache.clear();

	}

	private static Map<String, String> getProMapFromStream(String resourceName, InputStream is) {

		assert (is != null);
		Map<String, String> result = new HashMap<String, String>();
		try {

			UtilXmlSt.ElementInfo eInfo = new UtilXmlSt.ElementInfo("propertiy");
			eInfo.setReadAll(true);
			UtilXmlSt.getElementInfo(is, eInfo);

			List<UtilXmlSt.ElementValue> eList = eInfo.getElementValueList();
			if (UtilValidate.isNotEmpty(eList)) {
				for (UtilXmlSt.ElementValue ev : eList) {
					result.put(resourceName + ev.getAtt().get("name"), ev.value);
				}
			}

			// System.out.println(result);
			return result;
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					// e.printStackTrace();
					log.warn("close InputStream error", e);
				}
			}

		}

	}

	public static Map<String, String> getPropertiesMap(String resourceName) {

		String rName = getResourceName(resourceName);
		log.info("resourceName {} end path is {}",resourceName, rName);
		InputStream is = null;
		is = XmlProperty.class.getClassLoader().getResourceAsStream(rName);
		
		return getProMapFromStream(resourceName, is);

	}
	
	public static Map<String, String> getPropertiesMap(URL resourceName) {

		
		try {
			return getProMapFromStream(resourceName.toString(), resourceName.openStream());
		} catch (IOException e) {
			log.error("getProMap by url error",e);
            throw new RuntimeException(e);
		}

	}
}
